﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts; 
using Common;

public partial class website_admin_system_regioneditor : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Expires = -1;
        PageBegin();


        /// 保存设置
        if (RequestBase.GetFormString("__EVENTTARGET") == "save")
        {
            Save();
        }

        if (!IsPostBack)
        {
            CategoryDataBind();

            if (RequestBase.GetQueryString("otype") != "add")
                Initialize();
            else
            {
                if (RequestBase.GetQueryString("parentid") != "")
                    selParentList.Value = RequestBase.GetQueryString("parentid");
            }
        }
    }

    #region 类别列表数据绑定
    void CategoryDataBind()
    {
        DataTable dt = BLL.CommonBLL.Select("RegionBase", "*", " AND Layer IN(1,2) AND DeletionStateCode = 0 ORDER BY RegionName ASC");
        DropdownlistDataBind(1, 0, dt);
    }
    /// <summary>
    /// 类别列表邦定
    /// </summary>
    /// <param name="intLayer">级别</param>
    /// <param name="parentId">父目录ID</param>
    /// <param name="dt">目录数据表</param>
    void DropdownlistDataBind(int intLayer, int parentId, DataTable dt)
    {
        if (dt == null)
            return;

        DataRow[] dr = dt.Select("Layer = " + intLayer.ToString() + " AND ParentId = " + parentId.ToString());

        ArrayList al = new ArrayList();
        ListItem li;

        string str = ""; int intCategoryLayerID = 0;
        foreach (DataRow d in dr)
        {
            str = "　| ";
            intCategoryLayerID = int.Parse(d["Layer"].ToString());
            if (intLayer == 1)
                str = " + ";
            for (int i = 1; i < intLayer * intLayer; i++)
                str += "--";

            li = new ListItem("\t" + str + "\t" + d["RegionName"].ToString(), d["RegionId"].ToString());
            selParentList.Items.Add(li);
            DropdownlistDataBind(intCategoryLayerID + 1, int.Parse(d["RegionId"].ToString().Trim()), dt);
        }
    }
    #endregion

    #region 类别数据初始化
    void Initialize()
    {
        Model.RegionBase _obj = BLL.RegionBase.GetObjById(int.Parse(RequestBase.GetQueryString("regionid")));
        RegionObj = _obj;
    }

    public Model.RegionBase RegionObj
    {
        get
        {
            Model.RegionBase regionObj = new Model.RegionBase();

            if (!string.IsNullOrEmpty(RequestBase.GetQueryString("regionid")))
                regionObj.RegionId = int.Parse(RequestBase.GetQueryString("regionid"));

            regionObj.RegionName = txtRegionName.Value.Trim(); 
            regionObj.ParentId = int.Parse(selParentList.Value.Trim());  
            regionObj.RegionNameEng = txtRegionNameEng.Value.Trim(); 

            return regionObj;
        }
        set
        {
            if (value != null)
            {
                txtRegionName.Value = value.RegionName.Trim(); 
                selParentList.Value = value.ParentId.ToString().Trim();
                txtOriginalParentId.Value = value.ParentId.ToString();
                txtRegionNameEng.Value = value.RegionNameEng.ToString(); 
            }
        }
    }
    #endregion

    #region 保存数据
    void Save()
    {
        string res = "";

        if (RequestBase.GetQueryString("otype").Trim() == "add")
        {
            res = BLL.RegionBase.CreateNew(RegionObj);
        }

        if (RequestBase.GetQueryString("otype").Trim() == "modify")
        {
            res = BLL.RegionBase.Modify(RegionObj, (selParentList.Value.Trim() == txtOriginalParentId.Value.Trim()) ? false : true);
        }

        switch (res.Split('|')[0])
        {
            case "succeeded":
                JScript.AlertAndReturnValueToParent("保存成功!", string.Format("window.location.href='regionmagviewer.aspx?groupid={0}';", RequestBase.GetQueryString("groupid")));
                break; 
            default:
                JScript.Alert(res);
                break;
        }
    }
    #endregion
}
